import babel from "rollup-plugin-babel";
// CommonJS
import commonjs from "rollup-plugin-commonjs";
// 压缩使用
import { terser } from "rollup-plugin-terser";

import replace from 'rollup-plugin-replace';

import {nodeResolve} from "@rollup/plugin-node-resolve";


const PACKAGE_NAME = `vwaver-cloud`;

const plugins = [
  babel({
    exclude: "node_modules/**",
    runtimeHelpers: true,
  }),
  commonjs()
];

if (process.env.NODE_ENV == "dev") {
  plugins.splice(0, 0, replace({
    "window.process?.env?.NODE_ENV": JSON.stringify(process.env.NODE_ENV)
  }));
} else {
  plugins.push(removeConsole());
}

const sourcemap = process.env.NODE_ENV == "dev" ? "inline" : false;

// 去除console插件
function removeConsole() {
  return {
    name: "remove-console",
    transform(code, id) {
      const Reg = /console\.log\(.*\)/gi;
      return code.replace(Reg, "");
    },
  };
}

export default {
  input: "./src/index.js",
  output: [
    {
      // es6模块
      file: `./dist/${PACKAGE_NAME}.esm.js`,
      format: "es",
      sourcemap,
    },
    {
      // es6模块 minify
      file: `./dist/${PACKAGE_NAME}.esm.min.js`,
      format: "es",
      plugins: [terser()],
    },
    {
      // commonjs模块
      file: `./dist/${PACKAGE_NAME}.cjs.js`,
      format: "cjs",
      sourcemap,
    },
    {
      // commonjs模块 minify
      file: `./dist/${PACKAGE_NAME}.cjs.min.js`,
      format: "cjs",
      plugins: [terser()],
    },
    {
      // global模块
      file: `./dist/${PACKAGE_NAME}.global.js`,
      format: "iife",
      name: "VWaverCloud",
    },
    {
      // global模块 minify
      file: `./dist/${PACKAGE_NAME}.global.min.js`,
      format: "iife",
      name: "VWaverCloud"
    },
  ],
  plugins,
  external: [],
};
